package com.cndw.kungfu.model.siege.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.kungfu.vo.siege.PlayerRankingVO;

/**
 * 帮派，个人积分记录
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 * @since 1.0 Create on 2012-2-23
 */
public class SiegeRanking implements Serializable, Comparable<SiegeRanking> {
	private static final long serialVersionUID = -4394592955319295652L;
	private long id;
	private int ranking;
	private String name;
	private int score;
	private Map<Long, PlayerRanking> players;

	public SiegeRanking(long id, String name) {
		this.id = id;
		this.name = name;
		this.players = new ConcurrentHashMap<Long, PlayerRanking>();
	}

	public SiegeRanking(long id, String name, int ranking, int score) {
		this.id = id;
		this.name = name;
		this.ranking = ranking;
		this.score = score;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getRanking() {
		return ranking;
	}

	public void setRanking(int ranking) {
		this.ranking = ranking;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public Map<Long, PlayerRanking> getPlayers() {
		return players;
	}

	public void setPlayers(Map<Long, PlayerRanking> players) {
		this.players = players;
	}

	public synchronized void alterScore(int score) {
		this.score += score;
	}

	public void setPlayerRanking() {
		int i = 0;
		Set<PlayerRanking> rankings = new TreeSet<PlayerRanking>(players.values());
		for (PlayerRanking r : rankings) {
			i++;
			r.setRanking(i);
		}
	}

	public List<PlayerRankingVO> getPlayersVO() {
		int i = 0;
		List<PlayerRankingVO> voList = new ArrayList<PlayerRankingVO>();
		Set<PlayerRanking> rankings = new TreeSet<PlayerRanking>(players.values());
		for (PlayerRanking p : rankings) {
			i++;
			p.setRanking(i);
			voList.add(p.toPlayerRankingVO());
		}
		return voList;
	}

	@Override
	public int compareTo(SiegeRanking o) {
		if (o.getScore() > getScore()) {
			return 1;
		} else {
			return -1;
		}
	}

	public SiegeRanking clone() {
		return new SiegeRanking(this.id, this.name, this.ranking, this.score);
	}
}
